import torch
import torchvision
from torch import nn
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter

dataset = torchvision.datasets.CIFAR10(root='./CIFAR10', train=False, transform=torchvision.transforms.ToTensor(),
                                       download=True)

dataloader = DataLoader(dataset, batch_size=64)


# input = torch.tensor([[1, 2, 0, 3, 1],
#                       [0, 1, 2, 3, 1],
#                       [1, 2, 1, 0, 0],
#                       [5, 2, 3, 1, 1],
#                       [2, 1, 0, 1, 1]])
#
# input = torch.reshape(input, (-1, 1, 5, 5))
# print(input.shape)


class Tudui(nn.Module):
    def __init__(self):
        super(Tudui, self).__init__()
        # 空洞卷积（最大池化）只保留卷积核中的最大值，ceil_mode决定是向上还是向下取整
        # 目的是取得特征的同时减少训练参数，训练的更快
        self.maxpool1 = nn.MaxPool2d(kernel_size=3, ceil_mode=False)

    def forward(self, x):
        output = self.maxpool1(x)
        return output


tudui = Tudui()
# output = tudui(input)
# print(output)

writer = SummaryWriter('./logs/maxpool')
step = 0
for data in dataloader:
    imgs, targets = data
    writer.add_images('input', imgs, step)
    output = tudui(imgs)
    writer.add_images("output", output, step)
    step += 1


writer.close()